package edu.gyu.myshop.service.impl;

import edu.gyu.myshop.model.*;
import edu.gyu.myshop.dao.MyorderDao;
import edu.gyu.myshop.service.CartService;
import edu.gyu.myshop.service.GoodsService;
import edu.gyu.myshop.service.MyorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gyu.myshop.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class MyorderServiceImpl extends ServiceImpl<MyorderDao, Myorder> implements MyorderService {
    @Autowired
    CartService cartService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderDetailService orderDetailService;

    @Override
    @Transactional
    public boolean putOrder(HttpSession session) {
        Myuser user = (Myuser) session.getAttribute("user");
        List<Cart> carts = cartService.listCart(session);
        Myorder order = new Myorder();
        if (carts != null) {
            if (carts.size() > 0) {
                double total = 0.;
                for (Cart cart : carts) {

                    int buyNum = cart.getShoppingnum();
                    double price=cart.getGoodsPrice();
                    total+=price*buyNum;
                }
                order.setUserid(user.getId());
                order.setTotal(BigDecimal.valueOf(total));
                order.setCreateTime(LocalDateTime.now());
                this.save(order);


                for (Cart cart : carts) {
                    int goodsId = cart.getGoodsId();
                    int buyNum = cart.getShoppingnum();
                    int userId = user.getId();


                    //消减商品库存 ,留给订单事务
                    Goods buyGoods = goodsService.lambdaQuery()
                            .eq(Goods::getId, goodsId)
                            .one();
                    int storeNum = buyGoods.getStore();
                    storeNum -= buyNum;
                    if (storeNum >= 0) {
                        goodsService.lambdaUpdate()
                                .eq(Goods::getId, goodsId)
                                .set(Goods::getStore, storeNum)
                                .update();


                    }

                    OrderDetail orderDetail=new OrderDetail();
                    orderDetail.setUserid(userId);
                    orderDetail.setGoodsid(goodsId);
                    orderDetail.setPrice(BigDecimal.valueOf(cart.getGoodsPrice()));
                    orderDetail.setBuyNum(cart.getShoppingnum());
                    orderDetail.setOrderid(order.getId());
                    orderDetail.setCreateTime(LocalDateTime.now());
                    orderDetailService.save(orderDetail);
                }


                //清空用户购物车记录
                cartService.lambdaUpdate()
                        .set(Cart::getMyuserId,user.getId())
                        .remove();

//                System.out.println(1/0);
            }
        }

        return false;
    }

    @Override
    public List<Myorder> listOrder(HttpSession session) {
        Myuser user = (Myuser) session.getAttribute("user");
        List<Myorder> orders=null;
        if (user != null) {
            int uid = user.getId();
            orders = this.lambdaQuery()
                    .eq(Myorder::getUserid, uid)
                    .orderByDesc(Myorder::getId)
                    .list();

        }

        return orders;
    }


}